import React, { Component } from "react";
import { createPage } from "nc-lightapp-front";
import {
  buttonClick,
  initTemplate,
  afterEvent,
  refreshButtonClick,
} from "./events";
import {
  ProfileStyle,
  ProfileHead,
  ProfileBody,
  ButtonGroup,
  EmptyArea
} from "src/erm/sscrppublic/common/components/profile";
import { EditTable } from "src/erm/sscrppublic/common/components/table";
import "./index.less";
import pubMessage from "src/erm/sscrppublic/common/utils/pubMessage";

/**
 * ~~发票合并规则~~
 */
class SingleTable extends Component {
  constructor(props) {
    super();
    initTemplate.call(this, props);
    this.state = {
      status: "browse",
      // 勾选的数组
      checkArr: [],
      // 多选框列显隐
      showCheck: true
    };
    this.pubMessage = new pubMessage(props);
  }

  componentWillMount() {
    window.onbeforeunload = () => {
      let status = this.props.editTable.getStatus(window.bodycode);
      if (status == "edit") {
        return "";
      }
    };
  }

  render() {
    // let multiLang = this.props.MutiInit.getIntl(2011);
    let props = this.props;
    let multiLang = props.MutiInit.getIntl(2011)

    return (
      /*档案风格布局*/
      <ProfileStyle layout="singleTable" {...this.props}>
        {/*页面头*/}
        <ProfileHead
          title={multiLang.get('201103INME-0001')}
          refreshButtonEvent={refreshButtonClick.bind(this, props)}
          status={this.state.status}
        >
          <ButtonGroup
            area="page_header"
            buttonLimit="3"
            buttonEvent={buttonClick.bind(this)}
          />
        </ProfileHead>

        {/*页面体*/}
        <ProfileBody>
          
          <EmptyArea>
            <div className="header-intro">
              <span>{multiLang.get('201103INME-0002')}</span>
              <p>{multiLang.get('201103INME-0003')}</p>
            </div>
            
          </EmptyArea>

          <EditTable
            areaId="page_header"
            showCheck = {this.state.showCheck}
            // 单个勾选触发的函数
            onSelected = {(props, moduleId, record, index, selected) => {
              let checkArrNew = this.state.checkArr;
              // 勾选
              if(selected){
                checkArrNew.push(record);
              }
              // 取消勾选
              else{
                // 唯一主键
                let pkErminputtaxRule = _.get(record, "values.pk_erminvoicemerge.value");
                checkArrNew.forEach((item, index) =>{
                  let pkErminputtaxRuleI = _.get(item, "values.pk_erminvoicemerge.value");
                  if(pkErminputtaxRule === pkErminputtaxRuleI){
                    checkArrNew.splice(index,1);
                  }
                });
              }
              this.setState({checkArr: checkArrNew});
            }}
            // 全选
            onSelectedAll = {(props, moduleId, selected, len) => {
              if(selected){
                let dataAll = props.editTable.getAllRows(window.presetVar.bodycode);
                this.setState({checkArr: dataAll});
              }
              else{
                this.setState({checkArr: []});
              }
            }}
          >
          </EditTable>
        </ProfileBody>
      </ProfileStyle>
    );
  }
}

SingleTable = createPage({
  mutiLangCode: "2011",
})(SingleTable);

export default SingleTable;
